/*
 * Timer.c
 *
 * Created: 1/18/2013 6:00:56 PM
 *  Author: Hp-Minion
 */ 

#include <avr/io.h>
#include <avr/interrupt.h>
#include "General.h"
#include "Timer.h"
#include "Port.h"
#include "USART_0.h"
/*****************************************************************************************/
volatile unsigned long counter0 = 0;
volatile unsigned long counter00 = 0;
volatile unsigned long counter000 = 0;
volatile unsigned long counter1 = 0;
volatile unsigned long counter2 = 0;
volatile unsigned long counter20 = 0;
volatile unsigned long counter200 = 0;
volatile unsigned int pwm = 0;
/*****************************************************************************************/
void Init_timer0_us()
{
	cli();
	TCCR0A = 0;
	TCCR0B = 0;	
	//set prescaler to 64
	TCCR0B |= (1<<CS00) | (1<<CS01);
	TIMSK0 |= (1<<TOIE0);
	TCNT0 = 0;	
	counter0 = 0;
	sei();
}
/*****************************************************************************************/
void Init_timer0_ms()
{
	cli();
	TCCR0A = 0;
	TCCR0B = 0;
	//set prescaler to 256	
	TCCR0B |= (1<<CS02);
	TIMSK0 |= (1<<TOIE0);
	TCNT0 = 0;	
	counter0 = 0;
	sei();
}
/*****************************************************************************************/
void Init_timer0_s()
{
	cli();
	TCCR0A = 0;
	TCCR0B = 0;	
	TCCR0B |= (1<<CS00) | (1<<CS02);
	TIMSK0 |= (1<<TOIE0);
	TCNT0 = 0;	
	counter0 = 0;
	sei();
}
/*****************************************************************************************/
void Delay_us(unsigned int Value)
{
	unsigned long val;
	Init_timer0_us();
	
	val = Value / Freq_4us;
	
	while(counter0 != (val / 256)) {/*do nothing*/}
	while(TCNT0 != (val % 256)) {/*do nothing*/}
}
/*****************************************************************************************/
void Delay_ms(unsigned int Value)
{
	unsigned long val;
	Init_timer0_ms();
	
	val = (Value / Freq_16us)* 1000;
	
	while(counter0 != (val / 256)) {/*do nothing*/}
	while(TCNT0 != (val % 256)) {/*do nothing*/}	
}
/*****************************************************************************************/
void Start_Timer0()
{
	Init_timer0_us();
}
/*****************************************************************************************/
 unsigned long int Stop_Timer0()
{
	unsigned long time = 0;
	unsigned int buffer = 0;
	
	buffer = TCNT0;
	time = counter0 * 256 + buffer;
	
	return time;
}
/*****************************************************************************************/
void Init_timer2()
{
	cli();
	TCCR2A = 0;
	TCCR2B = 0;
	//set prescaler to 1024
	TCCR2B |= (1<<CS20) | (1<<CS22) | (1<<CS21);
	TIMSK2 |= (1<<TOIE2);
	TCNT2 = 0;	
	counter2 = 0;
	sei();
}
/*****************************************************************************************/
void Init_PWM1(void)
{
	TCCR1A = (1<<COM1B1) | (1<<WGM10) | (1<<WGM11);
	TCCR1B = (1<<CS10) | (1<<WGM12);
	TIMSK1 = (1<<OCIE1B);
	OCR1B = 1;
	PinMode(10,'D',OUTPUT);
}
/*****************************************************************************************/
void SetDutyCycle(unsigned int dty)
{
	OCR1B = map(dty, 0, 100, 0, PWM_MAX_BUFFER);
}
/*****************************************************************************************/
ISR(TIMER0_OVF_vect)
{
	counter0++;
	counter00++;
	counter000++;
}
/*****************************************************************************************/
ISR(TIMER1_COMPB_vect)
{
	counter1++;
}
/*****************************************************************************************/
ISR(TIMER2_OVF_vect)
{
	counter2++;
	counter20++;
	counter200++;
}
/*****************************************************************************************/
